<template>
    <div class="observerLibrary-increase">
        <!-- 新增弹窗 -->
        <a-modal
         :visible="visibleShow" 
         :title="addEditParameter.id?'编辑':'新增'" 
         @ok="submitHandle" 
         :confirmLoading="confirmLoading"
         @cancel="visibleShow = false">
            <a-spin size="large" :spinning="spinning">
                <a-form v-if="visibleShow" :form="form" :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }" @submit="submitHandle">
                    <a-form-item label="试卷题目">
                        <a-input
                        placeholder="请输入试卷题目"
                        v-decorator="[
                            'fsjtm', { initialValue:formState.fsjtm, rules: rules.fsjtm}
                        ]"/>
                    </a-form-item>
                    <a-form-item label="题目分数">
                        <a-input-number id="inputNumber" v-model="formState.ftmfs" :min="1" @change="forderChange" />
                    </a-form-item>
                    <a-form-item label="题目类型">
                        <a-select
                            placeholder="请选择题目类型"
                            v-decorator="[
                            'ftmlx',{ initialValue:formState.ftmlx, rules: rules.ftmlx}]"
                            
                        >
                            <a-select-option :value="item.value" v-for="(item,index) in optionsArr">
                                {{item.label}}
                            </a-select-option>
                        </a-select>
                    </a-form-item>
                </a-form>
            </a-spin>
        </a-modal>
    </div>
</template>
<script>
import {typeInput, typeNumber, typeSelect, typeCheckbox, typeRadio} from '@/components/compSearchCriteria/ContractType'
import compSelector from '@/components/compSelector/compSelector'
import validator from '@/common/validator'
import {subjectAddServe, subjectEditServe,ocalityPostAction} from './service/index'
export default {
    components:{
        compSelector
    },
    props:{
        addEditParameter:{
            type:Object,
            default:[]
        },
        cuerrfsjid:{
            type:String,
            default:''
        }

    },
    data(){
        return{
            spinning:true,
            form: this.$form.createForm(this, { name: 'topicReaseName' }),
            visibleShow:false,
            confirmLoading:false,
            typeSelect:typeSelect,
            formState:{fsjid:''},
            rules:{
                fsjtm: [{
                    required: true,
                    message: '请输入试卷题目',
                    trigger: 'blur',
                }],
                ftmlx: [{
                    required: true,
                    message: '请输入题目类型',
                    trigger: 'blur',
                }]
            },
            formRequer:'',
            optionsArr:[],
            submittedSuccessfully:false,
        }
    },
    
    watch:{
        addEditParameter(newval,oldVal){
            this.spinning = true
            //console.log('给了什么啊',newval)
            this.formState = {
                ...this.formState,
                fsjtm:newval.fsjtm?newval.fsjtm:'',
                ftmfs:newval.ftmfs?newval.ftmfs:1,
                ftmlx:newval.ftmlx?newval.ftmlx:'单选',
            }
            if(newval.id){
                this.formState.id = newval.id
            }else{
                this.formState.id = ''
            }
        },
        cuerrfsjid(newval,oldVal){
            //console.log('添加题目获取试卷id~~~~~~~~~~~~获取到试卷id',newval)
            this.formState.fsjid = newval
        }
    },
    mounted(){
        ocalityPostAction({dictName:'试卷题目类型'}).then(res=>{
                //console.log('字典的res',res)
                if(res.code == 200){
                    if(res.result.length){
                        this.optionsDataChange(res.result,{label:'itemText',value:'itemValue'})
                    }
                    
                }
            })
        
    },
    updated(){
        this.cancelLoading()
    },
    methods:{
        cancelLoading(){
            let timer = setInterval((val)=>{
                this.spinning = false
                clearInterval(timer);
            },500)
            
        },
        optionsDataChange(dataList,dataType){
            var newOptionArr = []
            //标识每一项的下标，并且添加到每一项
            dataList.forEach((item,index) => {
                 
                //console.log(newOptionArr.filter(l=>l.itemIndex === index))
                if(!newOptionArr.filter(l=>l.itemIndex === index).length){
                    newOptionArr.push({...item,itemIndex:index})
                }
            });
            //console.log('这是有下标的数组',newOptionArr)
            for (let i = 0; i <= newOptionArr.length; i++) {//打印的item为数组中的每一个对象
                let item = newOptionArr[i]
                let index = i
                for (let key in item) { //遍历数组中的对象
                    var currentItem = {}

                    //console.log('当前下标项',index === item.itemIndex)
                    //console.log(index)
                     
                    if(index === item.itemIndex){
                        currentItem = {
                            label:item[dataType.label],
                            value:item[dataType.value]
                        }
                        
                        if(!this.optionsArr.filter(l=>l.label === currentItem.label).length && currentItem.label){
                            this.optionsArr.push(currentItem)
                        }
                    }
                    

                }
            }
            //console.log('当前获取到的optionArr',this.optionsArr)
        },
        forderChange(val){
            //console.log(val)
            if(!val){
                this.formState.ftmfs = 1
            }
        },
        submitHandle() {
            //console.log(55555)
            var formState = {}
            this.form.validateFields((err, values) => {
                //console.log('验证表单')
                if (!err) {
                    this.confirmLoading = true
                    formState = {
                        ...this.formState,
                        ...values
                    }
                    //console.log('formState.id',formState.id)
                    if(formState.id){
                        subjectEditServe(formState).then(res=>{
                            if(res.code == 200){
                                this.submittedSuccessfully = true
                                this.$message.success('修改成功')
                                this.confirmLoading = false
                                this.visibleShow = false
                                this.$emit('handleOk',true,this.formState.id)
                            }else{
                                this.$message.warning(res.message)
                                this.confirmLoading = false
                            }
                        })
                    }else{
                        //console.log(66666666666)
                        subjectAddServe(formState).then(res=>{
                            if(res.code == 200){
                                this.$message.success('添加成功')
                                
                                this.submittedSuccessfully = true
                                this.confirmLoading = false
                                this.visibleShow = false
                                this.$emit('handleOk',true,'')
                            }else{
                                this.$message.warning(res.message)
                                this.confirmLoading = false
                            }
                        })
                    }
                    
                }
            });
            
        }
    }

}

</script>
<style>
/* .ant-form-item-label{
    font-weight: 600;
} */

 .ant-modal-title{
    font-weight: 600;
 }
 .ant-modal-header{
    background: #5ca3e5;
 }
 .ant-upload-list{
    display: none !important;
 }
 .ant-modal-footer .ant-btn-primary{
    border: 1px solid #5ca3e5 !important;
    background-color: #5ca3e5 !important;
    color: #fff !important;
 }
</style>